﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Resources;
using System.Globalization;

namespace JCard
{
    public partial class fExampleLearn : Form
    {
        #region Variables for resource
        ResourceManager objResourceManager;
        CultureInfo objCulInfo;
        #endregion

        #region Variables
        ArrayList arrExampleJP;
        ArrayList arrExampleVN;
        ArrayList arrExampleJP_Backup;
        ArrayList arrExampleVN_Backup;
        ArrayList arrExampleJP_Wrong;
        ArrayList arrExampleVN_Wrong;
        string curJP;
        string curVN;
        int intLearned = 0;
        int intRight = 0;
        int intTotal = 0;
        string strStatus;
        bool isChecked = false;
        #endregion

        #region Constructors
        public fExampleLearn()
        {
            InitializeComponent();
        }

        public fExampleLearn(ArrayList arrExampleJP, ArrayList arrExampleVN)
        {
            InitializeComponent();

            this.arrExampleJP = arrExampleJP;
            this.arrExampleVN = arrExampleVN;

            arrExampleJP_Wrong = new ArrayList();
            arrExampleVN_Wrong = new ArrayList();

            arrExampleJP_Backup = (ArrayList)arrExampleJP.Clone();
            arrExampleVN_Backup = (ArrayList)arrExampleVN.Clone();

            SetDisplayLabel();
        }

        private void SetDisplayLabel()
        {
            objResourceManager = new ResourceManager("JCard.Resources", typeof(fCLesson).Assembly);
            objCulInfo = new CultureInfo(Common.GetConfigValue(Constants.CONFIG_LANGUAGE_KEY, Constants.CONFIG_LANGUAGE_VALUE));

            strStatus = Common.GetResourceValue(Constants.RES_LEARN_EX_RESULT_NAME,
                    objCulInfo, objResourceManager, Constants.RES_LEARN_EX_RESULT_VALUE);
        }
        #endregion

        #region Loading
        private void fExampleLearn_Load(object sender, EventArgs e)
        {
            intTotal = arrExampleJP.Count;
            DisplayRandom();
            btnDisplay.Enabled = true;
            btnNext.Enabled = false;
            lblStatus.Text = String.Format(strStatus, intTotal, intRight, intLearned);
        }

        private void DisplayRandom()
        {
            Random rd = new Random();
            int curIdx = rd.Next(0, arrExampleJP.Count - 1);
            curJP = arrExampleJP[curIdx].ToString();
            curVN = arrExampleVN[curIdx].ToString();

            arrExampleJP.RemoveAt(curIdx);
            arrExampleVN.RemoveAt(curIdx);

            lblExampleVN.ForeColor = Color.Black;
            lblExampleVN.Text = curVN;
            txtExampleJP.ReadOnly = false;
            txtExampleJP.ForeColor = Color.Black;
            txtExampleJP.Clear();
            txtExampleJP.Focus();
            isChecked = false;
        }
        #endregion

        #region Xử lý chuyển đổi input language
        private void txtExampleJP_Enter(object sender, EventArgs e)
        {
            Common.ChangeInputLanguage(Constants.INPUT_JP);
        }

        private void txtExampleJP_Leave(object sender, EventArgs e)
        {
            Common.ChangeInputLanguage(Constants.INPUT_EN);
        }
        #endregion

        #region Xử lý khi học ví dụ
        private void button1_Click(object sender, EventArgs e)
        {
            intLearned++;
            
            if (txtExampleJP.Text.Equals(curJP.Substring(1).Trim()))
            {
                intRight++;
                txtExampleJP.ForeColor = Color.Blue;
                txtExampleJP.ReadOnly = true;
                btnNext.Enabled = true;
                btnNext.Focus();
            }
            else
            {
                arrExampleJP_Wrong.Add(curJP);
                arrExampleVN_Wrong.Add(curVN);
                lblExampleVN.ForeColor = Color.Blue;
                lblExampleVN.Text = curJP + Environment.NewLine + curVN;
                txtExampleJP.ForeColor = Color.Red;
                txtExampleJP.Focus();
            }

            lblStatus.Text = String.Format(strStatus, intTotal, intRight, intLearned);
            btnDisplay.Enabled = false;
            isChecked = true;            
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (arrExampleJP.Count > 0)
            {
                DisplayRandom();
                btnNext.Enabled = false;
                btnDisplay.Enabled = true;
            }
            else
            {
                if (arrExampleJP_Wrong.Count > 0)
                {
                    if (Common.ShowConfirmMsg(objCulInfo, objResourceManager,
                            Constants.RES_NOT_FINISH_LEARN_EX_NAME, Constants.RES_NOT_FINISH_LEARN_EX_VALUE,
                            intRight, intTotal - intRight, intTotal - intRight) == DialogResult.Yes)
                    {
                        arrExampleJP.AddRange(arrExampleJP_Wrong.ToArray());
                        arrExampleJP_Wrong.Clear();
                        arrExampleVN.AddRange(arrExampleVN_Wrong.ToArray());
                        arrExampleVN_Wrong.Clear();

                        intLearned = 0;
                        intRight = 0;
                        intTotal = arrExampleJP.Count;

                        DisplayRandom();
                        btnNext.Enabled = false;
                        btnDisplay.Enabled = true;

                        lblStatus.Text = String.Format(strStatus, intTotal, intRight, intLearned);
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {
                    if (Common.ShowConfirmMsg(objCulInfo, objResourceManager,
                            Constants.RES_FINISH_LEARN_EX_NAME, Constants.RES_FINISH_LEARN_EX_VALUE) == DialogResult.Yes)
                    {
                        arrExampleJP.AddRange(arrExampleJP_Backup.ToArray());
                        arrExampleVN.AddRange(arrExampleVN_Backup.ToArray());
                        intLearned = 0;
                        intRight = 0;
                        intTotal = arrExampleJP.Count;

                        DisplayRandom();
                        btnNext.Enabled = false;
                        btnDisplay.Enabled = true;

                        lblStatus.Text = String.Format(strStatus, intTotal, intRight, intLearned);
                    }
                    else
                    {
                        this.Close();
                    }
                }
            }

        }

        private void txtExampleJP_TextChanged(object sender, EventArgs e)
        {
            if (isChecked)
            {
                if (txtExampleJP.Text.Equals(curJP.Substring(1).Trim()))
                {
                    txtExampleJP.ForeColor = Color.Blue;
                    txtExampleJP.ReadOnly = true;
                    btnNext.Enabled = true;
                    btnNext.Focus();
                }
            }
        }
        #endregion
    }
}
